using System;
using System.Collections.Generic;
using System.Linq;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;


namespace SpaceEngine.Assets
{

    public class Asset3D : DrawableGameComponent
    {
        public Model assetModel;
        protected Vector3 rotation;
        protected Vector3 position;
        String path;
        Game1 game;
        float scale;

        public Asset3D(Game1 game, String assetPath, float scale)
            : base(game)
        {
            path = assetPath;
            position = Vector3.Zero;
            rotation = Vector3.Zero;
            this.game = game;
            this.scale = scale;
        }

        public override void Initialize()
        {
            base.Initialize();
        }

        protected override void LoadContent()
        {
            base.LoadContent();
            assetModel = Game.Content.Load<Model>(path);
        }

        public override void Update(GameTime gameTime)
        {
            
            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            // Copy any parent transforms.
            Matrix[] transforms = new Matrix[assetModel.Bones.Count];
            assetModel.CopyAbsoluteBoneTransformsTo(transforms);


            /* BASIC RENDERING */
            foreach (ModelMesh mesh in assetModel.Meshes)
            {
                // This is where the mesh orientation is set, as well as our camera and projection.
                foreach (BasicEffect effect in mesh.Effects)
                {
                    effect.EnableDefaultLighting();
                    effect.DirectionalLight0.Enabled = true;
                    effect.DirectionalLight1.Enabled = true;
                    effect.DirectionalLight2.Enabled = true;
                    effect.Alpha = 1.0f;
                    effect.SpecularPower = 10.0f;

                    effect.World = transforms[mesh.ParentBone.Index]
                        * Matrix.CreateRotationX(rotation.X)
                        * Matrix.CreateRotationY(rotation.Y)
                        * Matrix.CreateRotationZ(rotation.Z)
                        * Matrix.CreateTranslation(position)
                        * Matrix.CreateScale(scale);
                    effect.View = game.camera.View;
                    effect.Projection = game.camera.Projection;
                }
                mesh.Draw();
             }
            base.Draw(gameTime);
        }

        public Vector3 Position
        {
            get { return position; }
            set { position = value; }
        }

        public Vector3 Rotation
        {
            get { return rotation; }
            set { rotation = value; }
        }
    }
}